import { pick } from '@/utils/helper';
import { getHead, mergeHead } from './common';

// Open graph protocol
function getVideoOGMeta(data) {
  return [
    {
      property: 'og:type',
      content: 'video'
    },
    {
      property: 'og:video',
      content: data.videoUrl
    },
    {
      property: 'og:video:secure_url',
      content: data.videoUrl
    },
    {
      property: 'og:video:type',
      content: 'video/mp4'
    }
  ];
}

// Google Search Structure Data
function getVideoStructureDataHeadMeta(data) {
  return [
    {
      type: 'application/ld+json',
      json: {
        '@context': 'https://schema.org',
        '@type': 'VideoObject',
        mainEntityOfPage: {
          '@type': 'WebPage',
          '@id': data.canonicalPageUrl
        },
        ...pick({
          name: 'title',
          description: 'description',
          thumbnailUrl: 'thumb',
          uploadDate: 'uploadDate',
          duration: 'duration',
          contentUrl: 'videoUrl'
        })(data),
        interactionStatistic: {
          '@type': 'InteractionCounter',
          interactionType: {
            '@type': 'http://schema.org/WatchAction'
          },
          userInteractionCount: data.playCount || 0
        },
        publisher: {
          '@type': 'Organization',
          name: data.siteName,
          logo: {
            '@type': 'ImageObject',
            url: 'https://www.tnaot.com/img/logo-256.png'
          }
        }
      }
    }
  ];
}

export default function getVideoHead(video) {
  const data = {
    ...video,
    ...pick({
      siteName: 'siteName',
      pageLocale: 'pageLocale',
      pageUrl: 'pageUrl',
      canonicalPageUrl: 'canonicalPageUrl',
      desktopUrl: 'desktopUrl',
      title: 'title',
      thumb: 'thumbs.0',
      keyword: 'keyword',
      description: 'description',
      uploadDate: 'release_timestamp,date-iso',
      duration: 'duration,duration-iso-8601',
      videoUrl: 'video_url',
      playCount: 'play_count'
    })(video)
  };

  return mergeHead(
    getHead(data),
    { script: [...getVideoStructureDataHeadMeta(data)] },
    { meta: getVideoOGMeta(data) }
  );
}
